
package com.waitingsnow.common.share.qqMicroBlog.api
{
	import com.waitingsnow.common.share.qqMicroBlog.event.WeiboEvent;
	import com.waitingsnow.common.share.qqMicroBlog.operation.IRequestOperation;
	import com.waitingsnow.common.share.util.UploadPostHelper;
	import com.waitingsnow.core.debug.Logger;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.FileReference;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.utils.ByteArray;
	
	/**
	 * 微博内容相关的API
	 * @langversion 3.0
	 * @playerversion Flash 9
	 * @playerversion AIR 1.1
	 *
	 */
	public class Content extends AbstractAPI
	{
		
		/**
		 * 1.获取一条微博数据
		 * @param id 微博id
		 * @return IRequestOperation实例
		 */		
		public function show(id:String):IRequestOperation
		{
			checkAccessToken();
			var url:String = config.urlBase+"t/show";
			var attachParms:Object = {};
			attachParms.id = id;
			return generateDataRequest(url,attachParms,config.requestMethod);
		}
		/**
		 * 2.发表一条微博
		 * @param content 微博内容
		 * @param clientip 用户ip(以分析用户所在地)
		 * @param jing 经度（可以填空）
		 * @param wei 纬度（可以填空）
		 * @return IRequestOperation实例
		 */		
		public function add(content:String,clientip:String="127.0.0.1",jing:String="",wei:String=""):IRequestOperation
		{
			checkAccessToken();
			var url:String = config.urlBase+"t/add";
			var attachParms:Object = {};
			attachParms.content = content;
			attachParms.clientip = clientip;
			attachParms.jing = jing;
			attachParms.wei = wei;
			return generateDataRequest(url,attachParms,"POST");
		}
		/**
		 * 3.删除一条微博
		 * @param id 微博id
		 * @return IRequestOperation实例
		 */		
		public function del(id:String):IRequestOperation
		{
			checkAccessToken();
			var url:String = config.urlBase+"t/del";
			var attachParms:Object = {};
			attachParms.id = id;
			return generateDataRequest(url,attachParms,"POST");
		}
		/**
		 * 4.转播一条微博
		 * @param reid 转播父结点微博id
		 * @param content 微博内容
		 * @param clientip 用户ip(以分析用户所在地)
		 * @param jing 经度（可以填空）
		 * @param wei 纬度（可以填空）
		 * @return IRequestOperation实例
		 */		
		public function reAdd(reid:String,content:String,clientip:String="127.0.0.1",jing:String="",wei:String=""):IRequestOperation
		{
			checkAccessToken();
			var url:String = config.urlBase+"t/re_add";
			var attachParms:Object = {};
			attachParms.reid = reid;
			attachParms.content = content;
			attachParms.clientip = clientip;
			attachParms.jing = jing;
			attachParms.wei = wei;
			return generateDataRequest(url,attachParms,"POST");
		}
		/**
		 * 5.回复一条微博
		 * @param reid 转播父结点微博id
		 * @param content 微博内容
		 * @param clientip 用户ip(以分析用户所在地)
		 * @param jing 经度（可以填空）
		 * @param wei 纬度（可以填空）
		 * @return IRequestOperation实例
		 */		
		public function reply(reid:String,content:String,clientip:String="127.0.0.1",jing:String="",wei:String=""):IRequestOperation
		{
			checkAccessToken();
			var url:String = config.urlBase+"t/reply";
			var attachParms:Object = {};
			attachParms.reid = reid;
			attachParms.content = content;
			attachParms.clientip = clientip;
			attachParms.jing = jing;
			attachParms.wei = wei;
			return generateDataRequest(url,attachParms,"POST");
		}
		/**
		 * 6.发表一条带图片的微博
		 * @param content 微博内容
		 * @param file 上传的文件
		 * @param clientip 用户ip(以分析用户所在地)
		 * @param jing 经度（可以填空）
		 * @param wei 纬度（可以填空）
		 * @return IRequestOperation实例
		 */		
		public function addPic(content:String,file:FileReference,clientip:String="127.0.0.1",jing:String="",wei:String=""):IRequestOperation
		{
			checkAccessToken();
			var url:String = config.urlBase+"t/add_pic";
			var attachParms:Object = {};
			attachParms.content = content;
			attachParms.clientip = clientip;
			attachParms.jing = jing;
			attachParms.wei = wei;
			attachParms.Filename = file.name;
			attachParms.Upload = "Submit Query";
			return generateFileRequest(url,attachParms,"pic",file);
		}
        
        /**
         * 上传图片 
         * @param content
         * @param pic
         * 
         */        
        public function uploadPic(content:String, pic:ByteArray,clientip:String="127.0.0.1",jing:String="",wei:String=""):void
        {
            checkAccessToken();
            var url:String = config.urlBase+"t/add_pic";
            var attachParms:Object = {};
            attachParms.content = content;
            attachParms.clientip = clientip;
            attachParms.jing = jing;
            attachParms.wei = wei;
            attachParms.Filename = "temp.jpg";
            attachParms.Upload = "Submit Query";
            attachParms.format = "json";
            var urlVariables:URLVariables = generateUploadVar(url,attachParms);
            var req:URLRequest = new URLRequest(url);
            req.data = UploadPostHelper.getPostData(pic, urlVariables, "pic", UploadPostHelper.IMAGE_JPG);
            req.contentType = "multipart/form-data;" +"boundary=" +
                         UploadPostHelper.getBoundary();
            req.method = URLRequestMethod.POST;
            var upLoader:URLLoader = new URLLoader();
            upLoader.load(req);
            upLoader.addEventListener(Event.COMPLETE, upLoaderCompleteHandler);
            upLoader.addEventListener(IOErrorEvent.IO_ERROR, upLoaderIoErrorHandler);
        }
        
		/**
		 * 7.获取转播数或点评数
		 * @param ids 微博id的列表，逗号间隔
		 * @param flag 0获取转发计数，1点评计数2 两者都取
		 * @return IRequestOperation实例
		 */		
		public function reCount(ids:String,flag:int=0):IRequestOperation
		{
			checkAccessToken();
			var url:String = config.urlBase+"t/re_count";
			var attachParms:Object = {};
			attachParms.ids = ids;
			attachParms.flag = flag;
			return generateDataRequest(url,attachParms,config.requestMethod);
		}
		/**
		 * 8.获取单条微博的转发或点评列表
		 * @param rootid 转发或者回复根结点id（源微博id）
		 * @param flag 标识0 转播列表，1点评列表2 点评与转播列表
		 * @param pageflag （根据dwtime），0：第一页，1：向下翻页，2向上翻页；
		 * @param pagetime 本页起始时间（第一页时填0，继续向下翻页：填上一次请求返回的最后一条记录时间）
		 * @param reqnum 每次请求记录的条数（1-20条）
		 * @param twitterid 1-100条填0,继续向下翻页，填上一次请求返回的最后一条记录id，翻页用
		 * @return IRequestOperation实例
		 */		
		public function reList(rootid:String,flag:int=0,pageflag:int=0,pagetime:int=0,reqnum:int=20,twitterid:String="0"):IRequestOperation
		{
			checkAccessToken();
			var url:String = config.urlBase+"t/re_list";
			var attachParms:Object = {};
			attachParms.rootid = rootid;
			attachParms.flag = flag;
			attachParms.pageflag = pageflag;
			attachParms.pagetime = pagetime;
			attachParms.reqnum = reqnum;
			attachParms.twitterid = twitterid;
			return generateDataRequest(url,attachParms,config.requestMethod);
		}
		/**
		 * 9.点评一条微博
		 * @param reid 转播父结点微博id
		 * @param content 微博内容
		 * @param clientip 用户ip(以分析用户所在地)
		 * @param jing 经度（可以填空）
		 * @param wei 纬度（可以填空）
		 * @return IRequestOperation实例
		 */		
		public function comment(reid:String,content:String,clientip:String="127.0.0.1",jing:String="",wei:String=""):IRequestOperation
		{
			checkAccessToken();
			var url:String = config.urlBase+"t/comment";
			var attachParms:Object = {};
			attachParms.reid = reid;
			attachParms.content = content;
			attachParms.clientip = clientip;
			attachParms.jing = jing;
			attachParms.wei = wei;
			return generateDataRequest(url,attachParms,"POST");
		}
		/**
		 * 10.发表一条音乐微博
		 * @param content 微博内容
		 * @param musicURL 音乐地址
		 * @param title 音乐名
		 * @param author 演唱者
		 * @param clientip 用户ip(以分析用户所在地)
		 * @param jing 经度（可以填空）
		 * @param wei 纬度（可以填空）
		 * @return IRequestOperation实例
		 */		
		public function addMusic(content:String,musicURL:String,title:String,author:String,clientip:String="127.0.0.1",jing:String="",wei:String=""):IRequestOperation
		{
			checkAccessToken();
			var url:String = config.urlBase+"t/add_music";
			var attachParms:Object = {};
			attachParms.content = content;
			attachParms.url = musicURL;
			attachParms.title = title;
			attachParms.author = author;
			attachParms.clientip = clientip;
			attachParms.jing = jing;
			attachParms.wei = wei;
			return generateDataRequest(url,attachParms,"POST");
		}
		/**
		 * 11.发表一条视频微博
		 * @param content 微博内容
		 * @param videoURL 音乐地址
		 * @param clientip 用户ip(以分析用户所在地)
		 * @param jing 经度（可以填空）
		 * @param wei 纬度（可以填空）
		 * @return IRequestOperation实例
		 */		
		public function addVideo(content:String,videoURL:String,clientip:String="127.0.0.1",jing:String="",wei:String=""):IRequestOperation
		{
			checkAccessToken();
			var url:String = config.urlBase+"t/add_video";
			var attachParms:Object = {};
			attachParms.content = content;
			attachParms.url = videoURL;
			attachParms.clientip = clientip;
			attachParms.jing = jing;
			attachParms.wei = wei;
			return generateDataRequest(url,attachParms,"POST");
		}
		/**
		 * 12.获取视频信息
		 * @param videoURL 音乐地址
		 * @return IRequestOperation实例
		 */		
		public function getVideoInfo(videoURL:String):IRequestOperation
		{
			checkAccessToken();
			var url:String = config.urlBase+"t/getvideoinfo";
			var attachParms:Object = {};
			attachParms.url = videoURL;
			return generateDataRequest(url,attachParms,"POST");
		}
		/**
		 * 13.根据微博id批量获取微博内容（与索引合起来用）
		 * @param ids 微博id列表，用“,”隔开，如123456,456789（请求数量不要超过30个）
		 * @return IRequestOperation实例
		 */		
		public function list(ids:String):IRequestOperation
		{
			checkAccessToken();
			var url:String = config.urlBase+"t/list";
			var attachParms:Object = {};
			attachParms.ids = ids;
			return generateDataRequest(url,attachParms,config.requestMethod);
		}
		/**
		 * 14.获取转播的再次转播数
		 * @param ids 微博id列表，用“,”隔开，如123456,456789（请求数量不要超过30个）
		 * @return IRequestOperation实例
		 */		
		public function subReCount(ids:String):IRequestOperation
		{
			checkAccessToken();
			var url:String = config.urlBase+"t/sub_re_count";
			var attachParms:Object = {};
			attachParms.ids = ids;
			return generateDataRequest(url,attachParms,config.requestMethod);
		}
		/**
		 * 15.发表心情帖子
		 * @param content 微博内容
		 * @param signtype 心情类型（1，2，3，4，5）
		 * @param clientip 用户ip(以分析用户所在地)
		 * @param jing 经度（可以填空）
		 * @param wei 纬度（可以填空）
		 * @return IRequestOperation实例
		 */		
		public function addEmotion(content:String,signtype:int=1,clientip:String="127.0.0.1",jing:String="",wei:String=""):IRequestOperation
		{
			checkAccessToken();
			var url:String = config.urlBase+"t/add_emotion";
			var attachParms:Object = {};
			attachParms.content = content;
			attachParms.signtype = signtype;
			attachParms.clientip = clientip;
			attachParms.jing = jing;
			attachParms.wei = wei;
			return generateDataRequest(url,attachParms,"POST");
		}
		/**
		 * 16.用图片URL发表带图片的微博
		 * @param content 微博内容
		 * @param picUrl 图片的URL地址（URL最长不能超过1024字节）
		 * @param clientip 用户ip(以分析用户所在地)
		 * @param jing 经度（可以填空）
		 * @param wei 纬度（可以填空）
		 * @return IRequestOperation实例
		 */		
		public function addPicUrl(content:String,picUrl:String,clientip:String="127.0.0.1",jing:String="",wei:String=""):IRequestOperation
		{
			checkAccessToken();
			var url:String = config.urlBase+"t/add_pic_url";
			var attachParms:Object = {};
			attachParms.content = content;
			attachParms.pic_url = picUrl;
			attachParms.clientip = clientip;
			attachParms.jing = jing;
			attachParms.wei = wei;
			return generateDataRequest(url,attachParms,"POST");
		}
		/**
		 * 17.多类型发表
		 * @param content 微博内容
		 * @param picUrl 图片URL，可填空（URL最长为1024字节）
		 * @param videoUrl 视频URL，可填空（URL最长为1024字节）
		 * @param musicUrl 音乐URL，可填空（如果填写该字段，则music_title和music_author都必须填写）
		 * @param musicTitle 歌曲名 （UTF8编码）
		 * @param musicAuthor 歌曲作者（UTF8编码）
		 * @param clientip 用户ip(以分析用户所在地)
		 * @param jing 经度（可以填空）
		 * @param wei 纬度（可以填空）
		 * @return IRequestOperation实例
		 */		
		public function addMulti(content:String,picUrl:String="",videoUrl:String="",musicUrl:String="",musicTitle:String="",musicAuthor:String="",clientip:String="127.0.0.1",jing:String="",wei:String=""):IRequestOperation
		{
			checkAccessToken();
			var url:String = config.urlBase+"t/add_multi";
			var attachParms:Object = {};
			attachParms.content = content;
			attachParms.pic_url = picUrl;
			attachParms.video_url = videoUrl;
			attachParms.music_url = musicUrl;
			attachParms.music_title = musicTitle;
			attachParms.music_author = musicAuthor;
			attachParms.clientip = clientip;
			attachParms.jing = jing;
			attachParms.wei = wei;
			return generateDataRequest(url,attachParms,"POST");
		}
        
        private function upLoaderCompleteHandler(event:Event):void
        {
			Logger.debug(URLLoader(event.target).data);
            dispatchEvent(new WeiboEvent(WeiboEvent.UPLOAD_PIC_COMPLETE));
        }
        
        private function upLoaderIoErrorHandler(event:IOErrorEvent):void
        {
			Logger.error(event.text);
            dispatchEvent(new WeiboEvent(WeiboEvent.UPLOAD_PIC_ERROR));
        }
		
	}
}